﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataInput;

namespace DataInputUI
{
    public partial class InputControl : ContainerControl
    {
        public event EventHandler fieldChanged = null;
        public InputControl()
        {
            InitializeComponent();
        }

        Dictionary<String, Field> fields = new Dictionary<String, Field>();

        public InputControl(List<Group> groups, int groupWidth )
        {
            this.GroupWidth = groupWidth;
            setFields(groups);
        }

        public int GroupWidth { get; set; }

        public void setFields(List<Group> groups)
        {
            this.SuspendLayout();
            InitializeComponent();
            this.Controls.Clear();
            this.fields.Clear();            
            int top = 15;
            int startingTop = 15;
            int left = 10;
            int spacing = 5;
            float groupHeight = 0;
            List<int> LastColumnHeight = new List<int>();
            int column = 0;
            
            for ( int i = 0; i < groups.Count; i++ )
            {
                int fieldNum = 0;
                for (fieldNum = 0; (fieldNum < groups[i].Fields.Count && fields.ContainsKey(groups[i].Fields[fieldNum].FieldName)); fieldNum++)
                    ;
                if (fieldNum < groups[i].Fields.Count)
                {
                    top = column < LastColumnHeight.Count ? LastColumnHeight [ column ] + 10 : startingTop;
                    GroupBox group = new GroupBox();
                    group.Text = groups[i].GroupName;
                    group.Top = top;
                    group.Left = left;
                    group.Width = GroupWidth;
                    groupHeight = 0;
                    top += group.Font.Height + 10;
                    groupHeight += group.Font.Height + 10;
                    for (int j = 0; j < groups[i].Fields.Count; j++)
                        if (!fields.ContainsKey(groups[i].Fields[j].FieldName))
                        {
                            Field key = groups[i].Fields[j];
                            Label fieldNameLbl = new Label();
                            fieldNameLbl.Text = key.FieldLabel;
                            fieldNameLbl.Top = top;
                            fieldNameLbl.Left = left + 10;
                            fieldNameLbl.Width = GroupWidth - 20;
                            fieldNameLbl.Font = new Font(fieldNameLbl.Font, FontStyle.Bold);
                            fieldNameLbl.Height = fieldNameLbl.Font.Height + 3;
                            top += fieldNameLbl.Height;
                            groupHeight += fieldNameLbl.Height;
                            this.fields.Add(key.FieldName, key);
                            this.Controls.Add(fieldNameLbl);

                            float height = 0;
                            Control field = ControlFactory.generateControl(key);
                            field.TextChanged += new EventHandler(field_TextChanged);
                            if (field is ListControl)
                                ((ListControl) field).SelectedValueChanged += new EventHandler(field_TextChanged);
                            field.Top = top;
                            field.Left = left + 10;
                            field.Width = GroupWidth - 20;
                            if (field is ListControl)
                            {
                                if (field is ListBox)
                                {
                                    ListBox list = (ListBox) field;
                                    height = (list.Items.Count > 6 ? 6 : list.Items.Count)*(list.ItemHeight) + 4F;
                                }
                            }
                            else
                            {
                                height = field.Font.Height;
                            }
                            field.Height = (int) height;
                            field.Tag = key.FieldName;
                            top += field.Height;
                            groupHeight += field.Height;
                            if (j < groups[i].Fields.Count - 1)
                            {
                                groupHeight += spacing;
                                top += spacing;
                            }
                            this.Controls.Add(field);
                        }
                    group.Height = (int) groupHeight + 7;
                    if (column >= LastColumnHeight.Count)
                        LastColumnHeight.Add(group.Top+group.Height);
                    else
                        LastColumnHeight[column] = group.Top + group.Height;
                    this.Controls.Add(group);
                    group.SendToBack();
                    left += GroupWidth + 10;
                    if ( left + GroupWidth > Width )
                    {
                        left = 10;
                        column = 0;
                    }
                    else 
                        column++;
                }
            }
            //this.Height = top - 20;
            this.ResumeLayout();            
        }

        void field_TextChanged(object sender, EventArgs e)
        {
            Control j = (Control)sender;
            String fieldName = j.Tag.ToString();
            if (fields[fieldName].getOptions() == null)
                fields[fieldName].Value = j.Text;
            else
            {
                if (fields[fieldName] is MultiListField)
                {
                    List<Object> selectedValues = new List<Object> ();
                    fields[fieldName].Value = selectedValues;
                    ListBox currentListBox = (ListBox) j;
                    for (int i = 0; i < currentListBox.SelectedItems.Count; i++ )
                        selectedValues.Add( fields[fieldName].getOptions()[currentListBox.SelectedItems [ i ].ToString()]);
                }
                else
                    fields[fieldName].Value = fields[fieldName].getOptions()[j.Text];
            }
            if (fieldChanged != null)
                fieldChanged(fields[fieldName], new EventArgs());
        }
       
        public String isComplete()
        {
            foreach (KeyValuePair<String, Field> de in fields)
            {
                if (!de.Value.isValid())
                    return de.Value.FieldLabel;
            }
            return null;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
    }
}
